10 add vector
Adding local vector data (e.g., shp, geojson, kml) to the map
Uncomment the following line to install leafmap if needed.
In [1]:
Copied!
# !pip install leafmap
# !pip install leafmap
In [2]:
Copied!
import leafmap
import leafmap
In [3]:
Copied!
m = leafmap.Map()
m
m = leafmap.Map()
m
Out[3]:
Make this Notebook Trusted to load map: File -> Trust Notebook
This demo is based on the ipyleaflet plotting backend. The folium plotting backend does not have the interactive GUI for loading local vector data.

Add a GeoJSON to the map.
In [4]:
Copied!
m = leafmap.Map(center=[0, 0], zoom=2)
in_geojson = 'https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/cable_geo.geojson'
m.add_geojson(in_geojson, layer_name="Cable lines")
m
m = leafmap.Map(center=[0, 0], zoom=2)
in_geojson = 'https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/cable_geo.geojson'
m.add_geojson(in_geojson, layer_name="Cable lines")
m
Out[4]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Add a GeoJSON with random filled color to the map.
In [5]:
Copied!
m = leafmap.Map(center=[0, 0], zoom=2)
url = "https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/countries.geojson"
m.add_geojson(
url, layer_name="Countries", fill_colors=['red', 'yellow', 'green', 'orange']
)
m
m = leafmap.Map(center=[0, 0], zoom=2)
url = "https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/countries.geojson"
m.add_geojson(
url, layer_name="Countries", fill_colors=['red', 'yellow', 'green', 'orange']
)
m
Out[5]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Use the style_callbackfunction for assigning a random color to each polygon.
In [6]:
Copied!
import random
m = leafmap.Map(center=[0, 0], zoom=2)
url = "https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/countries.geojson"
def random_color(feature):
return {
'color': 'black',
'fillColor': random.choice(['red', 'yellow', 'green', 'orange']),
}
m.add_geojson(url, layer_name="Countries", style_callback=random_color)
m
import random
m = leafmap.Map(center=[0, 0], zoom=2)
url = "https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/countries.geojson"
def random_color(feature):
return {
'color': 'black',
'fillColor': random.choice(['red', 'yellow', 'green', 'orange']),
}
m.add_geojson(url, layer_name="Countries", style_callback=random_color)
m
Out[6]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Use custom style and hover_style functions.
In [7]:
Copied!
m = leafmap.Map(center=[0, 0], zoom=2)
url = "https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/countries.geojson"
style = {
"stroke": True,
"color": "#0000ff",
"weight": 2,
"opacity": 1,
"fill": True,
"fillColor": "#0000ff",
"fillOpacity": 0.1,
}
hover_style = {"fillOpacity": 0.7}
m.add_geojson(url, layer_name="Countries", style=style, hover_style=hover_style)
m
m = leafmap.Map(center=[0, 0], zoom=2)
url = "https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/countries.geojson"
style = {
"stroke": True,
"color": "#0000ff",
"weight": 2,
"opacity": 1,
"fill": True,
"fillColor": "#0000ff",
"fillOpacity": 0.1,
}
hover_style = {"fillOpacity": 0.7}
m.add_geojson(url, layer_name="Countries", style=style, hover_style=hover_style)
m
Out[7]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Add a shapefile to the map.
In [8]:
Copied!
m = leafmap.Map(center=[0, 0], zoom=2)
in_shp = '../data/countries.shp'
m.add_shp(in_shp, layer_name="Countries")
m
m = leafmap.Map(center=[0, 0], zoom=2)
in_shp = '../data/countries.shp'
m.add_shp(in_shp, layer_name="Countries")
m
Out[8]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Add a KML to the map.
In [9]:
Copied!
m = leafmap.Map()
in_kml = '../data/us_states.kml'
m.add_kml(in_kml, layer_name="US States KML")
m
m = leafmap.Map()
in_kml = '../data/us_states.kml'
m.add_kml(in_kml, layer_name="US States KML")
m
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Cell In [9], line 4 1 m = leafmap.Map() 3 in_kml = '../data/us_states.kml' ----> 4 m.add_kml(in_kml, layer_name="US States KML") 6 m File /opt/hostedtoolcache/Python/3.9.14/x64/lib/python3.9/site-packages/leafmap/foliumap.py:1340, in Map.add_kml(self, in_kml, layer_name, info_mode, **kwargs) 1337 if not os.path.exists(in_kml): 1338 raise FileNotFoundError("The provided KML could not be found.") -> 1340 data = kml_to_geojson(in_kml) 1342 self.add_geojson(data, layer_name=layer_name, info_mode=info_mode, **kwargs) File /opt/hostedtoolcache/Python/3.9.14/x64/lib/python3.9/site-packages/leafmap/common.py:2231, in kml_to_geojson(in_kml, out_geojson) 2227 import geopandas as gpd 2229 # import fiona 2230 # print(fiona.supported_drivers) -> 2231 gpd.io.file.fiona.drvsupport.supported_drivers["KML"] = "rw" 2232 gdf = gpd.read_file(in_kml, driver="KML") 2234 if out_geojson is not None: AttributeError: 'NoneType' object has no attribute 'drvsupport'
The add_vector function supports any vector data format supported by GeoPandas.
In [10]:
Copied!
m = leafmap.Map(center=[0, 0], zoom=2)
url = "https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/countries.geojson"
m.add_vector(
url, layer_name="Countries", fill_colors=['red', 'yellow', 'green', 'orange']
)
m
m = leafmap.Map(center=[0, 0], zoom=2)
url = "https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/countries.geojson"
m.add_vector(
url, layer_name="Countries", fill_colors=['red', 'yellow', 'green', 'orange']
)
m
Out[10]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [11]:
Copied!
m = leafmap.Map()
m = leafmap.Map()
In [12]:
Copied!
in_shp = '../data/countries.shp'
in_geojson = '../data/us_states.json'
in_kml = '../data/us_states.kml'
in_shp = '../data/countries.shp'
in_geojson = '../data/us_states.json'
in_kml = '../data/us_states.kml'
In [13]:
Copied!
m.add_shp(in_shp, layer_name="Shapefile")
m.add_shp(in_shp, layer_name="Shapefile")
In [14]:
Copied!
m.add_geojson(in_geojson, layer_name="GeoJSON")
m.add_geojson(in_geojson, layer_name="GeoJSON")
In [15]:
Copied!
m.add_kml(in_kml, layer_name="KML")
m.add_kml(in_kml, layer_name="KML")
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Cell In [15], line 1 ----> 1 m.add_kml(in_kml, layer_name="KML") File /opt/hostedtoolcache/Python/3.9.14/x64/lib/python3.9/site-packages/leafmap/foliumap.py:1340, in Map.add_kml(self, in_kml, layer_name, info_mode, **kwargs) 1337 if not os.path.exists(in_kml): 1338 raise FileNotFoundError("The provided KML could not be found.") -> 1340 data = kml_to_geojson(in_kml) 1342 self.add_geojson(data, layer_name=layer_name, info_mode=info_mode, **kwargs) File /opt/hostedtoolcache/Python/3.9.14/x64/lib/python3.9/site-packages/leafmap/common.py:2231, in kml_to_geojson(in_kml, out_geojson) 2227 import geopandas as gpd 2229 # import fiona 2230 # print(fiona.supported_drivers) -> 2231 gpd.io.file.fiona.drvsupport.supported_drivers["KML"] = "rw" 2232 gdf = gpd.read_file(in_kml, driver="KML") 2234 if out_geojson is not None: AttributeError: 'NoneType' object has no attribute 'drvsupport'
In [16]:
Copied!
m
m
Out[16]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Last update:
2022-10-06